package com.projects.xenia.documents_classes;

import java.util.Calendar;
import java.util.Date;
import java.util.UUID;

import android.content.ContentValues;
import android.database.Cursor;

import com.projects.xenia.global_modules.MainGlobalModule;
import com.projects.xenia.ref_classes.CustomersRefItem;
import com.projects.xenia.ref_classes.CustomersRefObject;
import com.projects.xenia.ref_classes.EmployeesRefItem;
import com.projects.xenia.ref_classes.EmployeesRefObject;


public class Documents_Task {

	public DocHeader Head;
	public DocLine Line;
	
	
	public boolean BackupIsExists () {
		
		Cursor _result = MainGlobalModule.DBE.rawQuery("select id from sxdoc_task_h where id = ?", new String [] {Head.id});
		return (_result.getCount()>0);
		
	}
	
	public void RestoreDocument () {
	
		MainGlobalModule.DBE.beginTransaction();
		
		MainGlobalModule.DBE.delete("xdoc_tasks_h", "id = ?", new String [] {Head.id});
		MainGlobalModule.DBE.execSQL("insert into xdoc_tasks_h select * from sxdoc_tasks_h where id = ?", new String [] {Head.id});
		
		MainGlobalModule.DBE.delete("xdoc_tasks_t", "hdoc_id = ?",  new String [] {Head.id});
		MainGlobalModule.DBE.execSQL("insert into xdoc_tasks_t select * from sxdoc_tasks_t where hdoc_id = ?", new String [] {Head.id});		
			
		MainGlobalModule.DBE.setTransactionSuccessful();
		MainGlobalModule.DBE.endTransaction();
		
	}
	
	public void BackupDocument () {
		
		MainGlobalModule.DBE.beginTransaction();
		
		MainGlobalModule.DBE.delete("sxdoc_tasks_h", null, null);
		MainGlobalModule.DBE.execSQL("insert into sxdoc_tasks_h select * from xdoc_tasks_h where id = ?", new String [] {Head.id});
		
		MainGlobalModule.DBE.delete("sxdoc_payments_t", null, null);
		MainGlobalModule.DBE.execSQL("insert into sxdoc_tasks_t select * from xdoc_tasks_t where hdoc_id = ?", new String [] {Head.id});		
			
		MainGlobalModule.DBE.setTransactionSuccessful();
		MainGlobalModule.DBE.endTransaction();
		
	}

	
	public class DocHeader {
		
		public String id; 
		public int docnum; 
		public Date date_create; 
		public Date date_save; 
		public Date date_target;
		public int state; 
		public int verstamp;
		
		public EmployeesRefItem user;		
		public EmployeesRefItem employee;
		public CustomersRefItem customer;
		
		public String task_descr;
		public int task_state;		
			
	}
	
	
	public class DocLine {
		
		public String id;
		public String hdoc_id;		
		public String task_id;
		public String task_descr;
		public int task_state;
		
	}
	
	
	public void NewDoc () {
		
		Head = new DocHeader ();
		Line = new DocLine ();
		
		Head.id = UUID.randomUUID().toString().toUpperCase();
		Head.docnum = GetLastDocNum ();			
		Head.date_create = Calendar.getInstance().getTime();
		Head.date_save = Calendar.getInstance().getTime();
		Head.date_target = Calendar.getInstance().getTime();
		Head.state = 0;
		Head.verstamp = 0;		
		Head.user = new EmployeesRefObject(MainGlobalModule.SettingsCollection.get("ManagerId")).RefItem;
		Head.employee = new EmployeesRefObject(MainGlobalModule.SettingsCollection.get("ManagerId")).RefItem;		
		Head.customer = null;
		Head.task_descr = null;
		Head.task_state = 0;
		
		// 0 - new , 1 - closed
		
	}
	
	public void OpenDoc (String id) {
		
		Head = new DocHeader ();
		
		Cursor DocumentsResult = MainGlobalModule.DBE.rawQuery("select * from xdoc_tasks_h where id = ?",new String [] {id});		
		DocumentsResult.moveToFirst();
	
		Head.id = DocumentsResult.getString(DocumentsResult.getColumnIndex("id"));
		Head.docnum = DocumentsResult.getInt(DocumentsResult.getColumnIndex("docnum"));			
		Head.date_create = MainGlobalModule.SqlStringToDate(DocumentsResult.getString(DocumentsResult.getColumnIndex("date_create")));
		Head.date_save = MainGlobalModule.SqlStringToDate(DocumentsResult.getString(DocumentsResult.getColumnIndex("date_save")));
		Head.date_target = MainGlobalModule.SqlStringToDate(DocumentsResult.getString(DocumentsResult.getColumnIndex("date_target")));
		Head.state = DocumentsResult.getInt(DocumentsResult.getColumnIndex("state"));
		Head.verstamp = DocumentsResult.getInt(DocumentsResult.getColumnIndex("verstamp"));
		
		Head.user = new EmployeesRefObject(DocumentsResult.getString(DocumentsResult.getColumnIndex("user_id"))).RefItem;
		Head.employee = new EmployeesRefObject(DocumentsResult.getString(DocumentsResult.getColumnIndex("employees_id"))).RefItem;		
		Head.customer = new CustomersRefObject(DocumentsResult.getString(DocumentsResult.getColumnIndex("customers_id"))).RefItem;
		
		Head.task_descr = DocumentsResult.getString(DocumentsResult.getColumnIndex("id"));;
		Head.task_state = DocumentsResult.getInt(DocumentsResult.getColumnIndex("task_state"));
		
		BackupDocument();
		
		Head.verstamp ++;
		
	}
	
	public void FinishDoc () {
		
		if (Head.customer == null){
		
			MainGlobalModule.DBE.beginTransaction();
		
			MainGlobalModule.DBE.delete("xdoc_tasks_h", "id=?", new String []{Head.id});
			MainGlobalModule.DBE.delete("xdoc_tasks_t", "hdoc_id=?", new String []{Head.id});
			
			MainGlobalModule.DBE.setTransactionSuccessful();
			MainGlobalModule.DBE.endTransaction();
		
		}
	}
	
	public void SaveDoc () {
		
		ContentValues InsertUpdateValues = new ContentValues ();
		InsertUpdateValues.put("id", Head.id);
		InsertUpdateValues.put("docnum", Head.docnum);		
		InsertUpdateValues.put("date_create", MainGlobalModule.DateToSqlString(Head.date_create));
		InsertUpdateValues.put("date_save", MainGlobalModule.DateToSqlString(Head.date_save));				
		InsertUpdateValues.put("date_target", MainGlobalModule.DateToSqlString(Head.date_target));
		InsertUpdateValues.put("state", Head.state);
		InsertUpdateValues.put("verstamp", Head.verstamp);
		InsertUpdateValues.put("user_id", Head.user.id);
		InsertUpdateValues.put("employees_id", Head.employee.id);
		
		if (Head.customer!=null) {
			InsertUpdateValues.put("customers_id", Head.customer.id);			
		}
		else {
			InsertUpdateValues.putNull("customers_id");
		}
		
		InsertUpdateValues.put("task_descr", Head.task_descr);
		InsertUpdateValues.put("task_state", Head.task_state);
				
		if (MainGlobalModule.DBE.update("xdoc_tasks_h", InsertUpdateValues, "id=?", new String []{Head.id})<0) {
			MainGlobalModule.DBE.insert("xdoc_tasks_h", null, InsertUpdateValues);	
		}
		
	}

	private int GetLastDocNum () {

		int DocNum = 0;
		Cursor ScalarResult = MainGlobalModule.DBE.rawQuery("select max(docnum) from xdoc_tasks_h", null);

		if (ScalarResult.getCount()>0) {
			ScalarResult.moveToFirst();
			DocNum = ScalarResult.getInt(0);								
		}

		ScalarResult.close();

		DocNum ++;

		return (DocNum);

	}
	
	public void SaveLine () {
		
		ContentValues InsertUpdateValues = new ContentValues ();
		InsertUpdateValues.put("id", Line.id);
		InsertUpdateValues.put("hdoc_id", Line.hdoc_id);		
		InsertUpdateValues.put("task_id", Line.task_id);				
		InsertUpdateValues.put("task_descr", Line.task_descr);
		InsertUpdateValues.put("task_state", Line.task_state);
				
		if (MainGlobalModule.DBE.update("xdoc_tasks_t", InsertUpdateValues, "id=?", new String []{Line.id})<0) {
			MainGlobalModule.DBE.insert("xdoc_tasks_t", null, InsertUpdateValues);	
		}
		
	}
	
	public boolean DeleteDoc () {
		
		try {
			
			Head.state = 3;
			SaveDoc ();
			
			return true;
			
		} catch (Exception e) {
			
			return false;
			
		}		
		
	}

	public void DeleteLine () {
		
		MainGlobalModule.DBE.delete("xdoc_tasks_t", "id=?", new String []{Line.id});
		
	}
	
	public boolean GetLineById (String id) {		

		Line = new DocLine();
		boolean _result = false;
		
		Cursor DocumentsResult = MainGlobalModule.DBE.rawQuery("select * from xdoc_tasks_t where id = ?",new String [] {id});
		
		if (DocumentsResult.getCount()>0) {
			
			DocumentsResult.moveToFirst();
			
			Line.id = DocumentsResult.getString(DocumentsResult.getColumnIndex("id"));
			Line.hdoc_id = DocumentsResult.getString(DocumentsResult.getColumnIndex("hdoc_id"));			
			Line.task_id = DocumentsResult.getString(DocumentsResult.getColumnIndex("task_id"));
			Line.task_descr = DocumentsResult.getString(DocumentsResult.getColumnIndex("task_descr"));
			Line.task_state = DocumentsResult.getInt(DocumentsResult.getColumnIndex("task_state"));
			
			_result = true;
			
		}		
		
		return (_result);
		
	}
	
}
